package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.BuyCartGoodsAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsAllPriceBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.BuyCartGoodsListModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.BuyCartGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.BuyOneGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCartAllGoodPriceModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetUserAddressByIdModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.UserInfoModel;

public class BuyCartGoodsActivity extends BaseActivity implements View.OnClickListener {
    private String good_id;
    private boolean up = false;
    private TextView buycart_shopname, buycart_allprice;
    TextView buycart_tjdd;
    EditText buycart_comment;
    double goodsprice;
    int goodsnum;
    private int nowsum;
    private String userid;
    private String useraddressid;




    private String shopid,shopname;
    TextView buycart_username, buycart_phone, buycart_address,buyone_flagnum;
    LinearLayout buycart_gotoaddresslist;
    ImageView buycart_fh;
    RecyclerView buycart_rv;
    private List<CartGoodsBean> items;

    public android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 1:
//                    CartPriceModel cartPriceModel=new CartPriceModel();
//                    cartPriceModel.showCartPrice(getid(),mrid,tListener);
                    //showToast("handler");
                   // mdialog = WeiboDialogUtils.createLoadingDialog(GetCartGoodsActivity.this, "加载中...");
                    GetCartAllGoodPriceModel getCartAllGoodPriceModel = new GetCartAllGoodPriceModel();
                    getCartAllGoodPriceModel.getCartAllGoodPrice(userid, shopid,cartGoodsAllPriceBeanTListener);
                    break;
            }
        }
    };


    @Override
    void initView() {
        setLayout_file(R.layout.activity_buy_cart_goods);
        shopid = getIntent().getStringExtra("shopid");
        shopname = getIntent().getStringExtra("shopname");
        buycart_rv = (RecyclerView)findViewById(R.id.buycart_rv);
        buyone_flagnum = (TextView)findViewById(R.id.buyone_flagnum);
       // Toast.makeText(BuyCartGoodsActivity.this,shopid,Toast.LENGTH_SHORT).show();
//        shop_name = getIntent().getStringExtra("shopname");
//        goodsnum = getIntent().getIntExtra("goodsnum", 0);

        buycart_username = (TextView) findViewById(R.id.buycart_username);
        buycart_phone = (TextView) findViewById(R.id.buycart_phone);
        buycart_address = (TextView) findViewById(R.id.buycart_address);
        buycart_shopname = (TextView) findViewById(R.id.buycart_shopname);
        buycart_fh = (ImageView)findViewById(R.id.buycart_fh);
        buycart_tjdd = (TextView)findViewById(R.id.buyone_tjdd);
        buycart_comment = (EditText)findViewById(R.id.buycart_comment);
        buycart_allprice = (TextView) findViewById(R.id.buyone_allprice);
        buycart_gotoaddresslist = (LinearLayout) findViewById(R.id.buycart_gotoaddresslist);


        buycart_shopname.setText(shopname);
        userid = getid();

        BuyCartGoodsListModel buyCartGoodsListModel = new BuyCartGoodsListModel();
        buyCartGoodsListModel.buyCartGoodsList(userid, shopid, new ListListener<CartGoodsBean>() {
            @Override
            public void onResponse(List<CartGoodsBean> list) {
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(BuyCartGoodsActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                buycart_rv.setLayoutManager(linearLayoutManager);
                items=list;
                BuyCartGoodsAdapter buyCartGoodsAdapter=new BuyCartGoodsAdapter(BuyCartGoodsActivity.this,items,R.layout.buycart_item,handler);
                buycart_rv.setAdapter(buyCartGoodsAdapter);
                buycart_rv.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFail(String msg) {

            }
        });
        GetCartAllGoodPriceModel getCartAllGoodPriceModel = new GetCartAllGoodPriceModel();
        getCartAllGoodPriceModel.getCartAllGoodPrice(userid, shopid,cartGoodsAllPriceBeanTListener);



        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.getUserInfo(userid, userInfoBeanTListener);
//
//
//        GetGoodsInfoModel getGoodsInfoModel = new GetGoodsInfoModel();
//        getGoodsInfoModel.getGoodsInfo(good_id, goodsBeanTListener);

    }

    @Override
    void initEvent() {
        buycart_gotoaddresslist.setOnClickListener(this);
//        buycart_add.setOnClickListener(this);
//        buycart_desc.setOnClickListener(this);
        buycart_tjdd.setOnClickListener(this);
        buycart_fh.setOnClickListener(this);
    }

    TListener<CartGoodsAllPriceBean> cartGoodsAllPriceBeanTListener = new TListener<CartGoodsAllPriceBean>() {
        @Override
        public void onResponse(CartGoodsAllPriceBean cartGoodsAllPriceBean) {
            buycart_allprice.setText("￥"+String.valueOf(cartGoodsAllPriceBean.getAllprice())+"元");
            buyone_flagnum.setText("共"+cartGoodsAllPriceBean.getBuyflag_count()+"件");
        }

        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    void initDate() {

    }

    TListener<UserInfoBean> userInfoBeanTListener = new TListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean userInfoBean) {
            useraddressid = userInfoBean.getUseraddress_id();
            GetUserAddressByIdModel getUserAddressByIdModel = new GetUserAddressByIdModel();
            getUserAddressByIdModel.getUserAddressById(useraddressid, userAddressBeanTListener1);
            //Toast.makeText(BuyOneGoodsActivity.this,useraddressid,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyCartGoodsActivity.this,"ku", Toast.LENGTH_SHORT).show();
        }
    };
//

    TListener<UserAddressBean> userAddressBeanTListener1 = new TListener<UserAddressBean>() {
        @Override
        public void onResponse(UserAddressBean userAddressBean) {

            if (userAddressBean.getUseraddress_id() == null){
                AlertDialog dlg = new AlertDialog.Builder(BuyCartGoodsActivity.this)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("你的收货地址为空，请添加收货地址")//显示类容
                        .setNegativeButton("取消",null)
                        .setPositiveButton("前往添加地址", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                userid = getid();
                                String orderaddress = buycart_address.getText().toString().trim();
                                String ordercomment = buycart_comment.getText().toString().trim();
                                String goodid = good_id;
                                String goodnum = String.valueOf(goodsnum);
                                String addressid = useraddressid;
                                BuyOneGoodsModel buyOneGoodsModel = new BuyOneGoodsModel();
                               // buyOneGoodsModel.buyOneGoods(userid,orderaddress,ordercomment,goodid,goodnum,addressid,successBeanTListener);
                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();
            }
            buycart_username.setText(userAddressBean.getUseraddress_username());
            buycart_address.setText(userAddressBean.getUseraddress_city()+userAddressBean.getUseraddress_address());
            buycart_phone.setText(userAddressBean.getUseraddress_phone());
            // useraddressid= userAddressBean.getUseraddress_id();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyCartGoodsActivity.this,"ccc",Toast.LENGTH_SHORT).show();
        }
    };

//
//    TListener<GoodsBean> goodsBeanTListener = new TListener<GoodsBean>() {
//        @Override
//        public void onResponse(GoodsBean goodsBean) {
//            String img = Service.BASE_URL + "flower_impression/public/uploads/" + goodsBean.getGood_photo();
//            Picasso.with(BuyCartGoodsActivity.this).load(img).into(buyone_goodimg);
//            buyone_goodname.setText(goodsBean.getGood_name());
//            buyone_goodprice.setText("￥" + String.valueOf(goodsBean.getGood_price()));
//            buyone_xiaojiprice.setText(String.valueOf(goodsnum * goodsBean.getGood_price()));
//            buyone_allprice.setText("￥" + String.valueOf(goodsnum * goodsBean.getGood_price()));
//            nowsum  = goodsBean.getGood_num();
//            goodsprice = goodsBean.getGood_price();
//        }
//
//        @Override
//        public void onFail(String msg) {
//
//        }
//    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buycart_gotoaddresslist:
                if (buycart_address.getText().toString().trim().equals("")){
                    Intent intent4 = new Intent(BuyCartGoodsActivity.this,AddAddressressActivity.class);
                    startActivity(intent4);
                    Toast.makeText(BuyCartGoodsActivity.this,"收货地址为空，请添加收货地址",Toast.LENGTH_SHORT).show();
                }
                else {
                    Intent intent = new Intent(BuyCartGoodsActivity.this, SelectUserAddressActivity.class);
                    intent.putExtra("useraddressid", useraddressid);
                    startActivity(intent);
                }
                break;
//
//            case R.id.buyone_add:
//                add();
//                break;
//            case R.id.buyone_desc:
//                desc();
//                break;
            case R.id.buyone_tjdd:
                AlertDialog dlg = new AlertDialog.Builder(BuyCartGoodsActivity.this)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("选择操作")//显示类容
                        .setNegativeButton("取消",null)
                        .setNeutralButton("稍后支付",null)
                        .setPositiveButton("支付宝", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                userid = getid();
                                String orderaddress = buycart_address.getText().toString().trim();
                                if (orderaddress.equals("")){
                                    orderaddress = "该用户没有备注信息";
                                }
                                String ordercomment = buycart_comment.getText().toString().trim();
                                //String goodid = good_id;
                                //String goodnum = String.valueOf(goodsnum);
                                String addressid = useraddressid;
                                if (useraddressid == null || orderaddress == null){
                                    Intent intent1 = new Intent(BuyCartGoodsActivity.this,AddAddressressActivity.class);
                                    startActivity(intent1);
                                    Toast.makeText(BuyCartGoodsActivity.this,"收货地址为空，请添加收货地址",Toast.LENGTH_SHORT).show();
                                }
                                else{
                                    BuyCartGoodsModel buyCartGoodsModel = new BuyCartGoodsModel();
                                    buyCartGoodsModel.buyCartGoods(getid(),shopid,orderaddress,ordercomment,useraddressid,successBeanTListener);
//                                    BuyOneGoodsModel buyOneGoodsModel = new BuyOneGoodsModel();
//                                    buyOneGoodsModel.buyOneGoods(userid,orderaddress,ordercomment,o,goodnum,addressid,successBeanTListener);
                                }

                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();
                break;
            case R.id.buycart_fh:
                finish();
                break;
        }
    }

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {

            if (successBean.getSuccess().equals("1")){
                Toast.makeText(BuyCartGoodsActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else{
                Toast.makeText(BuyCartGoodsActivity.this,"购买失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyCartGoodsActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }


}
